package com.stottlerhenke.samples.dynamicscriptintg;

/**
 * <p>Title: </p>
 * <p>Description: This is a sample project designed to show you how to build a very simple "Hello World" program using SimBionic.
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Stottler Henke</p>
 * @author Jeremy Ludwig
 * @version 1.0
 */


import java.util.ArrayList;

import com.stottlerhenke.dynamicscripting.DynamicScriptingWrapper;
import com.stottlerhenke.simbionic.api.SB_Exception;
import com.stottlerhenke.simbionic.api.SB_Interface;
import com.stottlerhenke.simbionic.api.SB_Param;



 /**
 * This class defines a simple interface between SimBionic and
 * our HelloWorld application.
 */
public class DS_Interface extends SB_Interface
{
	static public String lastSay = null;
	protected ArrayList<String> _transcript = new ArrayList<String>();
	
	public DS_Interface()
	{
	}
	
	public SB_Param doPredicate(int predId, long entityId, ArrayList params)
	{
		SB_Param returnValue = new SB_Param();
		
		/*    switch (predId)
		 {
		 case HelloWorldConstants.PRED_HappinessLevel:
		 {
		 // since we have no real simulation, just make up
		  // an arbitrary happiness level
		   
		   // set the return value for the predicate
		    returnValue.setInteger( 87 );
		    }
		    break;
		    }*/
		
		return returnValue;
	}
	
	public void doAction(int actionId, long entityId, ArrayList params) throws SB_Exception
	{
		switch (actionId)
		{
		case DS_Constants.ACTION_Say:
		{
			// extract the first parameter, which is of type "string"
			String message = ((SB_Param) params.get(0)).getString();
			
			// now print the string to the console
			lastSay = new String(message);
			
			//System.out.println(lastSay);
			
			if(lastSay == null || "".equals(lastSay))
				_transcript.clear();
			else
				_transcript.add(lastSay);
		}
		break;
		case DS_Constants.ACTION_Reward:
		{
			// extract the first parameter, which is of type "string"
			String state = ((SB_Param) params.get(0)).getString();
			
			double reward = 0;
			
			int lastSay = Integer.parseInt(DS_Interface.lastSay);
			if(lastSay == 0 || lastSay == 1)
				reward = 1.0;
			else
				reward = 0;
			
			//Special reward for episodic learning
			if(_transcript.size() > 1)
			{
				reward = 0;
				if(_transcript.contains("0"))
					reward += 10;
				
				if(_transcript.contains("1"))
					reward  += 10;
				
				if(_transcript.contains("2"))
					reward  -= 10;
			}
			
			DynamicScriptingWrapper.getInstance().rewardChoicePoint(state, reward);
			
			_transcript.clear();
		}
		break;
		}
	}
	
	public String getModuleName()
	{
		return new String("HelloWorld");
	}
	
	public ArrayList getModuleActions()
	{
		return new ArrayList();
	}
	
	public ArrayList getModulePredicates()
	{
		return new ArrayList();
	}
}